package com.kayang;

import com.kayang.common.config.RedisUtil;
import com.kayang.common.db.Database;
import com.kayang.common.db.DatabaseFactory;
import com.kayang.nadapt.table.DataRow;
import com.kayang.nadapt.table.DataTable;
import com.kayang.nadapt.util.LogHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;


@EnableDiscoveryClient
@SpringBootApplication
public class CommonApplication implements ApplicationRunner {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        try {
            SpringApplication.run(CommonApplication.class, args);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Autowired
    RedisUtil redisUtil;

    public void run(ApplicationArguments args) throws Exception {
        //test redis
        redisUtil.set("amos", "This is a test!", 60);
        System.out.println(redisUtil.get("amos"));
        Thread.sleep(3 * 1000);
        System.out.println(redisUtil.get("amos"));

        //test db
        Database db = DatabaseFactory.getDatabase();
        String xsql = "select top 1 * from Skysecuser";
        DataTable dt = db.queryTable(xsql);
        for (DataRow dr : dt.getRows()) {
            LogHelper.info(dr.get("id") + " - " + dr.get("account"));
        }
        LogHelper.info("Common Loaded OK");
    }

}
